package com.tjsinfo.tjsoa.system.bo;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ITSystemPostDAO;
import com.tjsinfo.tjsoa.system.vo.TSystemPost;

public class SystemPostBO implements ISystemPostBO {
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	private ITSystemPostDAO systemPostDAO;
	
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public ITSystemPostDAO getSystemPostDAO() {
		return systemPostDAO;
	}

	public void setSystemPostDAO(ITSystemPostDAO systemPostDAO) {
		this.systemPostDAO = systemPostDAO;
	}

	public String addSystemPost(TSystemPost systemPost) {
		// TODO Auto-generated method stub
		return systemPostDAO.save(systemPost);
	}

	public Boolean deleteSystemPost(String id) {
		// TODO Auto-generated method stub
		try {
			TSystemPost systemPost = selectById(id);
			systemPostDAO.delete(systemPost);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List<TSystemPost> selectAll() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemPost.class);
		Byte state = 0;
		criteria.add(Restrictions.eq("isDel", state));
		return qbcDao.getCriteria(criteria);
	}

	public TSystemPost selectById(String id) {
		// TODO Auto-generated method stub
		
		return systemPostDAO.findById(id);
	}

	public Boolean updateSystemPost(TSystemPost systemPost) {
		// TODO Auto-generated method stub
		try {
			hibernateDao.update(systemPost);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public List<TSystemPost> selectPostPage(Integer first, Integer max) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemPost.class);
		return qbcDao.findPageByCriteria(criteria, max, first).getItems();
	}

	public Integer selectTotalLine() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemPost.class);
		return qbcDao.getQueryNumber(criteria, "postId");
	}

	public boolean update(TSystemPost detachedInstance) {
		// TODO Auto-generated method stub
		return systemPostDAO.update(detachedInstance);
	}
	

	public TSystemPost findById(String id) {
		// TODO Auto-generated method stub
		return systemPostDAO.findById(id);
	}	

	public Boolean intoSystemPost(String postId){
		TSystemPost tsp = findById(postId);
		if (tsp.getIsDel() == 1) {
			Byte state = 0;
			tsp.setIsDel(state);
		} else {
			Byte state = 1;
			tsp.setIsDel(state);
		}
		return  update(tsp);
	}

	public Boolean isSameSystemPostName(String postname) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemPost.class);		
		criteria.add(Restrictions.eq("postName", postname));
		if (qbcDao.getCriteria(criteria).size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
